/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZEmailAddress;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZMessage.ZMimePart;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Enumeration;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestNotification
/*     */   extends TestCase
/*     */ {
/*  58 */   private static String RECIPIENT_NAME = "user1";
/*  59 */   private static String TAPPED_NAME = "user1";
/*  60 */   private static String INTERCEPTOR_NAME = "user2";
/*  61 */   private static String INTERCEPTOR2_NAME = "user3";
/*     */   
/*  63 */   private static String[] ALL_TEST_USERS = { "user1", "user2", "user3" };
/*     */   
/*  65 */   private static String NAME_PREFIX = TestNotification.class.getSimpleName();
/*     */   
/*     */   private boolean mOriginalReplyEnabled;
/*     */   private String mOriginalReply;
/*     */   private boolean mOriginalNotificationEnabled;
/*     */   private String mOriginalNotificationAddress;
/*     */   private String mOriginalNotificationSubject;
/*     */   private String mOriginalNotificationBody;
/*     */   private String[] mOriginalInterceptAddresses;
/*     */   private String mOriginalInterceptSendHeadersOnly;
/*     */   private String mOriginalSaveToSent;
/*  76 */   private final boolean mIsServerTest = false;
/*     */   
/*     */   protected void setUp()
/*     */     throws Exception
/*     */   {
/*  81 */     super.setUp();
/*  82 */     cleanUp();
/*     */     
/*  84 */     Account account = TestUtil.getAccount(RECIPIENT_NAME);
/*  85 */     this.mOriginalReplyEnabled = account.getBooleanAttr("zimbraPrefOutOfOfficeReplyEnabled", false);
/*  86 */     this.mOriginalReply = account.getAttr("zimbraPrefOutOfOfficeReply", "");
/*  87 */     this.mOriginalNotificationEnabled = account.getBooleanAttr("zimbraPrefNewMailNotificationEnabled", false);
/*  88 */     this.mOriginalNotificationAddress = account.getAttr("zimbraPrefNewMailNotificationAddress", "");
/*  89 */     this.mOriginalNotificationSubject = account.getAttr("zimbraNewMailNotificationSubject", "");
/*  90 */     this.mOriginalNotificationBody = account.getAttr("zimbraNewMailNotificationBody", "");
/*  91 */     this.mOriginalInterceptAddresses = account.getMultiAttr("zimbraInterceptAddress");
/*  92 */     this.mOriginalInterceptSendHeadersOnly = account.getAttr("zimbraInterceptSendHeadersOnly", "");
/*  93 */     this.mOriginalSaveToSent = account.getAttr("zimbraPrefSaveToSent", "");
/*     */   }
/*     */   
/*     */   public void testIntercept()
/*     */     throws Exception
/*     */   {
/*  99 */     String interceptorAddress = TestUtil.getAddress(INTERCEPTOR_NAME);
/* 100 */     TestUtil.setAccountAttr(TAPPED_NAME, "zimbraInterceptAddress", interceptorAddress);
/* 101 */     TestUtil.setAccountAttr(TAPPED_NAME, "zimbraInterceptSendHeadersOnly", "FALSE");
/*     */     
/*     */ 
/* 104 */     ZMailbox interceptorMbox = TestUtil.getZMailbox(INTERCEPTOR_NAME);
/* 105 */     ZMailbox tappedMbox = TestUtil.getZMailbox(TAPPED_NAME);
/* 106 */     String tappedAddress = TestUtil.getAddress(TAPPED_NAME);
/* 107 */     String subject = NAME_PREFIX + " testIntercept-receive";
/* 108 */     TestUtil.addMessageLmtp(subject, tappedAddress, interceptorAddress);
/*     */     
/* 110 */     ZMessage tappedMsg = TestUtil.getMessage(tappedMbox, "subject:\"" + subject + "\"");
/* 111 */     ZMessage interceptMsg = TestUtil.waitForMessage(interceptorMbox, "subject:\"" + subject + "\"");
/* 112 */     verifyInterceptMessage(interceptMsg, "add message", "Inbox", Integer.toString(2));
/* 113 */     compareContent(tappedMbox, tappedMsg, interceptorMbox, interceptMsg);
/*     */     
/*     */ 
/* 116 */     ZMailbox.ZOutgoingMessage outgoing = new ZMailbox.ZOutgoingMessage();
/* 117 */     List<ZEmailAddress> addresses = new ArrayList();
/* 118 */     addresses.add(new ZEmailAddress(TestUtil.getAddress(INTERCEPTOR_NAME), null, null, "t"));
/*     */     
/* 120 */     outgoing.setAddresses(addresses);
/* 121 */     subject = NAME_PREFIX + " testIntercept-draft-1";
/* 122 */     outgoing.setSubject(subject);
/* 123 */     outgoing.setMessagePart(new ZMailbox.ZOutgoingMessage.MessagePart("text/plain", "I always feel like somebody's watching me."));
/* 124 */     tappedMbox.saveDraft(outgoing, null, Integer.toString(6));
/* 125 */     tappedMsg = TestUtil.getMessage(tappedMbox, "in:drafts subject:\"" + subject + "\"");
/* 126 */     interceptMsg = TestUtil.waitForMessage(interceptorMbox, "subject:\"" + subject + "\"");
/* 127 */     verifyInterceptMessage(interceptMsg, "add message", "Drafts", Integer.toString(6));
/* 128 */     compareContent(tappedMbox, tappedMsg, interceptorMbox, interceptMsg);
/*     */     
/*     */ 
/* 131 */     ZMessage draft = TestUtil.getMessage(tappedMbox, "in:drafts subject:\"" + subject + "\"");
/* 132 */     subject = NAME_PREFIX + " testIntercept-draft-2";
/* 133 */     outgoing.setSubject(subject);
/* 134 */     tappedMbox.saveDraft(outgoing, draft.getId(), null);
/* 135 */     tappedMsg = TestUtil.getMessage(tappedMbox, "in:drafts subject:\"" + subject + "\"");
/* 136 */     interceptMsg = TestUtil.waitForMessage(interceptorMbox, "subject:\"" + subject + "\"");
/* 137 */     verifyInterceptMessage(interceptMsg, "save draft", "Drafts", Integer.toString(6));
/* 138 */     compareContent(tappedMbox, tappedMsg, interceptorMbox, interceptMsg);
/*     */     
/*     */ 
/* 141 */     TestUtil.setAccountAttr(TAPPED_NAME, "zimbraPrefSaveToSent", "TRUE");
/* 142 */     subject = NAME_PREFIX + " testIntercept-send-1";
/* 143 */     TestUtil.sendMessage(tappedMbox, INTERCEPTOR_NAME, subject);
/* 144 */     tappedMsg = TestUtil.waitForMessage(tappedMbox, "in:sent subject:\"" + subject + "\"");
/* 145 */     interceptMsg = TestUtil.waitForMessage(interceptorMbox, "subject:intercepted subject:\"" + subject + "\"");
/* 146 */     verifyInterceptMessage(interceptMsg, "add message", "Sent", Integer.toString(5));
/* 147 */     compareContent(tappedMbox, tappedMsg, interceptorMbox, interceptMsg);
/*     */     
/*     */ 
/* 150 */     TestUtil.setAccountAttr(TAPPED_NAME, "zimbraPrefSaveToSent", "FALSE");
/* 151 */     subject = NAME_PREFIX + " testIntercept-send-2";
/* 152 */     TestUtil.sendMessage(tappedMbox, INTERCEPTOR_NAME, subject);
/* 153 */     interceptMsg = TestUtil.waitForMessage(interceptorMbox, "subject:intercepted subject:\"" + subject + "\"");
/* 154 */     verifyInterceptMessage(interceptMsg, "send message", "none", "none");
/*     */     
/*     */ 
/* 157 */     TestUtil.setAccountAttr(TAPPED_NAME, "zimbraInterceptSendHeadersOnly", "TRUE");
/* 158 */     subject = NAME_PREFIX + " testIntercept-headers-only";
/* 159 */     TestUtil.sendMessage(interceptorMbox, TAPPED_NAME, subject);
/* 160 */     tappedMsg = TestUtil.waitForMessage(tappedMbox, "in:inbox subject:\"" + subject + "\"");
/* 161 */     interceptMsg = TestUtil.waitForMessage(interceptorMbox, "subject:intercepted subject:\"" + subject + "\"");
/* 162 */     verifyInterceptMessage(interceptMsg, "add message", "Inbox", Integer.toString(2));
/* 163 */     compareContent(tappedMbox, tappedMsg, interceptorMbox, interceptMsg);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testInterceptMultiValue()
/*     */     throws Exception
/*     */   {
/* 172 */     String interceptor1Address = TestUtil.getAddress(INTERCEPTOR_NAME);
/* 173 */     String interceptor2Address = TestUtil.getAddress(INTERCEPTOR2_NAME);
/* 174 */     String[] interceptorAddresses = { interceptor1Address, interceptor2Address };
/*     */     
/* 176 */     TestUtil.setAccountAttr(TAPPED_NAME, "zimbraInterceptAddress", interceptorAddresses);
/* 177 */     TestUtil.setAccountAttr(TAPPED_NAME, "zimbraInterceptSendHeadersOnly", "FALSE");
/*     */     
/*     */ 
/* 180 */     ZMailbox tappedMbox = TestUtil.getZMailbox(TAPPED_NAME);
/* 181 */     String tappedAddress = TestUtil.getAddress(TAPPED_NAME);
/* 182 */     String subject = NAME_PREFIX + " testIntercept-receive";
/* 183 */     TestUtil.addMessageLmtp(subject, tappedAddress, interceptor1Address);
/*     */     
/*     */ 
/* 186 */     ZMailbox interceptor1Mbox = TestUtil.getZMailbox(INTERCEPTOR_NAME);
/* 187 */     ZMailbox interceptor2Mbox = TestUtil.getZMailbox(INTERCEPTOR2_NAME);
/* 188 */     ZMessage tappedMsg = TestUtil.getMessage(tappedMbox, "subject:\"" + subject + "\"");
/* 189 */     ZMessage interceptMsg1 = TestUtil.waitForMessage(interceptor1Mbox, "subject:\"" + subject + "\"");
/* 190 */     ZMessage interceptMsg2 = TestUtil.waitForMessage(interceptor2Mbox, "subject:\"" + subject + "\"");
/* 191 */     verifyInterceptMessage(interceptMsg1, "add message", "Inbox", Integer.toString(2));
/* 192 */     verifyInterceptMessage(interceptMsg2, "add message", "Inbox", Integer.toString(2));
/* 193 */     compareContent(tappedMbox, tappedMsg, interceptor1Mbox, interceptMsg1);
/* 194 */     compareContent(tappedMbox, tappedMsg, interceptor2Mbox, interceptMsg2);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void verifyInterceptMessage(ZMessage interceptMsg, String operation, String folderName, String folderId)
/*     */     throws Exception
/*     */   {
/* 203 */     ZMessage.ZMimePart part = interceptMsg.getMimeStructure();
/* 204 */     assertEquals("multipart/mixed", part.getContentType());
/* 205 */     List<ZMessage.ZMimePart> children = part.getChildren();
/* 206 */     assertEquals(2, children.size());
/*     */     
/*     */ 
/* 209 */     ZMessage.ZMimePart bodyPart = (ZMessage.ZMimePart)children.get(0);
/* 210 */     assertEquals("text/plain", bodyPart.getContentType());
/* 211 */     String body = bodyPart.getContent();
/* 212 */     String context = "Unexpected body: \n" + body;
/* 213 */     assertTrue(context, body.contains("Intercepted message for " + RECIPIENT_NAME));
/* 214 */     assertTrue(context, body.contains("Operation=" + operation));
/* 215 */     assertTrue(context, body.contains("folder=" + folderName));
/* 216 */     assertTrue(context, body.contains("folder ID=" + folderId));
/*     */     
/*     */ 
/* 219 */     ZMessage.ZMimePart interceptedPart = (ZMessage.ZMimePart)children.get(1);
/* 220 */     assertEquals("message/rfc822", interceptedPart.getContentType());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void compareContent(ZMailbox tappedMbox, ZMessage tappedMsg, ZMailbox interceptorMbox, ZMessage interceptMsg)
/*     */     throws Exception
/*     */   {
/* 228 */     String relativeUrl = String.format("?id=%s&part=2", new Object[] { interceptMsg.getId() });
/* 229 */     InputStream in = interceptorMbox.getRESTResource(relativeUrl);
/* 230 */     String interceptedMsgContent = new String(ByteUtil.getContent(in, -1)).trim();
/* 231 */     String tappedMsgContent = TestUtil.getContent(tappedMbox, tappedMsg.getId()).trim();
/*     */     
/* 233 */     Account account = TestUtil.getAccount(TAPPED_NAME);
/*     */     
/*     */ 
/* 236 */     MimeMessage tappedMimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(tappedMsgContent.getBytes()));
/* 237 */     MimeMessage interceptedMimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(interceptedMsgContent.getBytes()));
/*     */     
/* 239 */     boolean headersOnly = account.getBooleanAttr("zimbraInterceptSendHeadersOnly", false);
/* 240 */     Set<String> tappedHeaderLines = getHeaderLines(tappedMimeMsg);
/* 241 */     Set<String> interceptedHeaderLines = getHeaderLines(interceptedMimeMsg);
/* 242 */     tappedHeaderLines.removeAll(getHeaderLines(interceptedMimeMsg));
/* 243 */     interceptedHeaderLines.removeAll(getHeaderLines(tappedMimeMsg));
/* 244 */     String context = "Unexpected headers found.  tapped: " + StringUtil.join(",", tappedHeaderLines) + ".  intercepted: " + StringUtil.join(",", interceptedHeaderLines) + ".";
/*     */     
/*     */ 
/* 247 */     assertTrue(context, (tappedHeaderLines.size() == 0) && (interceptedHeaderLines.size() == 0));
/*     */     
/*     */ 
/* 250 */     if (headersOnly) {
/* 251 */       String interceptedBody = new String(ByteUtil.getContent(interceptedMimeMsg.getInputStream(), 0));
/* 252 */       if (interceptedBody != null) {
/* 253 */         interceptedBody = interceptedBody.trim();
/*     */       }
/* 255 */       assertTrue("Unexpected body: '" + interceptedBody + "'", (interceptedBody == null) || (interceptedBody.length() == 0));
/*     */     } else {
/* 257 */       TestUtil.assertMessageContains(tappedMsgContent, interceptedMsgContent);
/*     */     }
/*     */   }
/*     */   
/*     */   private Set<String> getHeaderLines(MimeMessage msg) throws MessagingException
/*     */   {
/* 263 */     Set<String> headerLines = new HashSet();
/* 264 */     Enumeration<?> e = msg.getAllHeaderLines();
/* 265 */     while (e.hasMoreElements()) {
/* 266 */       headerLines.add((String)e.nextElement());
/*     */     }
/* 268 */     return headerLines;
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 274 */     cleanUp();
/*     */     
/*     */ 
/* 277 */     Account account = TestUtil.getAccount(RECIPIENT_NAME);
/*     */     
/* 279 */     Map<String, Object> attrs = new HashMap();
/* 280 */     attrs.put("zimbraPrefOutOfOfficeReplyEnabled", LdapUtil.getLdapBooleanString(this.mOriginalReplyEnabled));
/*     */     
/* 282 */     attrs.put("zimbraPrefOutOfOfficeReply", this.mOriginalReply);
/* 283 */     attrs.put("zimbraPrefNewMailNotificationEnabled", LdapUtil.getLdapBooleanString(this.mOriginalNotificationEnabled));
/*     */     
/* 285 */     attrs.put("zimbraPrefNewMailNotificationAddress", this.mOriginalNotificationAddress);
/* 286 */     attrs.put("zimbraNewMailNotificationSubject", this.mOriginalNotificationSubject);
/* 287 */     attrs.put("zimbraNewMailNotificationBody", this.mOriginalNotificationBody);
/* 288 */     if ((this.mOriginalInterceptAddresses != null) && (this.mOriginalInterceptAddresses.length == 0)) {
/* 289 */       attrs.put("zimbraInterceptAddress", "");
/*     */     } else {
/* 291 */       attrs.put("zimbraInterceptAddress", this.mOriginalInterceptAddresses);
/*     */     }
/* 293 */     attrs.put("zimbraInterceptSendHeadersOnly", this.mOriginalInterceptSendHeadersOnly);
/* 294 */     attrs.put("zimbraPrefSaveToSent", this.mOriginalSaveToSent);
/* 295 */     Provisioning.getInstance().modifyAttrs(account, attrs);
/*     */     
/* 297 */     super.tearDown();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void cleanUp()
/*     */     throws Exception
/*     */   {
/* 315 */     for (String userName : ALL_TEST_USERS) {
/* 316 */       TestUtil.deleteTestData(userName, NAME_PREFIX);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 322 */     TestUtil.cliSetup();
/* 323 */     TestUtil.runTest(TestNotification.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestNotification.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */